<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车</title>
    <style>
        /* 购物车样式 */
        .cart-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 20px;
            border-bottom: 1px solid #eee;
        }

        .clear-cart-btn {
            background: none;
            border: none;
            color: #666;
            cursor: pointer;
        }

        .clear-cart-btn:hover {
            color: #e4393c;
        }

        .cart-items {
            padding: 20px;
        }

        .cart-item {
            display: flex;
            align-items: center;
            padding: 15px 0;
            border-bottom: 1px solid #eee;
        }

        .item-checkbox {
            width: 30px;
        }

        .item-image {
            width: 100px;
            height: 100px;
            margin-right: 15px;
        }

        .item-image img {
            width: 100%;
            height: 100%;
            object-fit: contain;
        }

        .item-info {
            flex: 1;
            display: flex;
            flex-direction: column;
        }

        .item-info h3 {
            margin: 0 0 10px 0;
            font-size: 16px;
            font-weight: normal;
        }

        .item-price {
            color: #e4393c;
            font-weight: bold;
            margin-bottom: 10px;
        }

        .quantity-selector {
            display: flex;
            align-items: center;
            margin-bottom: 10px;
        }

        .quantity-selector button {
            width: 30px;
            height: 30px;
            border: 1px solid #ddd;
            background-color: #f9f9f9;
            cursor: pointer;
        }

        .quantity-selector input {
            width: 50px;
            height: 30px;
            text-align: center;
            border: 1px solid #ddd;
            border-left: none;
            border-right: none;
        }

        .remove-btn {
            background: none;
            border: none;
            color: #666;
            cursor: pointer;
            text-align: left;
            padding: 0;
            width: fit-content;
        }

        .remove-btn:hover {
            color: #e4393c;
        }

        .item-subtotal {
            width: 120px;
            text-align: right;
        }

        .item-subtotal.price {
            color: #e4393c;
            font-weight: bold;
        }

        .cart-summary {
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            background-color: white;
            border-top: 1px solid #ddd;
            padding: 15px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .total-price.price {
            color: #e4393c;
            font-size: 18px;
            font-weight: bold;
        }

        .checkout-btn {
            background-color: #e4393c;
            color: white;
            border: none;
            padding: 10px 30px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }

        .checkout-btn:disabled {
            background-color: #ccc;
            cursor: not-allowed;
        }

        .empty-cart {
            text-align: center;
            padding: 50px 0;
        }

        .empty-cart i {
            font-size: 60px;
            color: #ccc;
            margin-bottom: 20px;
            display: block;
        }

        .start-shopping-btn {
            background-color: #e4393c;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            margin-top: 20px;
        }

        /* 结算页面样式 */
        .checkout-modal {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: rgba(0, 0, 0, 0.5);
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 1000;
        }

        .checkout-container {
            background-color: white;
            width: 80%;
            max-width: 800px;
            max-height: 80vh;
            overflow-y: auto;
            border-radius: 4px;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
        }

        .checkout-title {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 20px;
            color: #333;
        }

        .checkout-section {
            margin-bottom: 20px;
            padding: 15px;
            border: 1px solid #eee;
            border-radius: 4px;
        }

        .section-title {
            font-size: 16px;
            margin-bottom: 15px;
            color: #666;
        }

        .address-list {
            margin-bottom: 15px;
        }

        .address-item {
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-bottom: 10px;
            cursor: pointer;
            transition: all 0.3s;
        }

        .address-item:hover {
            border-color: #1890ff;
        }

        .address-item.selected {
            border-color: #1890ff;
            background-color: #f0f7ff;
        }

        .address-item .default-tag {
            display: inline-block;
            background-color: #1890ff;
            color: white;
            padding: 2px 5px;
            font-size: 12px;
            border-radius: 2px;
            margin-right: 5px;
        }

        .address-item .address-detail {
            margin-top: 5px;
            color: #333;
        }

        .address-item .receiver {
            font-weight: bold;
        }

        .add-address-btn {
            color: #1890ff;
            cursor: pointer;
            display: inline-flex;
            align-items: center;
        }

        .checkout-form-item {
            margin-bottom: 15px;
        }

        .checkout-form-item label {
            display: block;
            margin-bottom: 5px;
            color: #666;
        }

        .checkout-form-item select,
        .checkout-form-item input,
        .checkout-form-item textarea {
            width: 100%;
            padding: 8px 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        .checkout-form-item textarea {
            min-height: 60px;
        }

        .checkout-products {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }

        .checkout-products th {
            background-color: #f5f5f5;
            padding: 10px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        .checkout-products td {
            padding: 10px;
            border-bottom: 1px solid #eee;
        }

        .checkout-summary {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 20px;
            padding-top: 15px;
            border-top: 1px solid #eee;
        }

        .checkout-total {
            color: #666;
        }

        .checkout-total .price {
            color: #e4393c;
            font-weight: bold;
        }

        .submit-order-btn {
            background-color: #1890ff;
            color: white;
            border: none;
            padding: 10px 30px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }

        .submit-order-btn:hover {
            background-color: #40a9ff;
        }
    </style>
</head>
<body>
<div id="cart-page" v-if="cartItems.length > 0">
    <div class="cart-header">
        <h2>我的购物车</h2>
        <button @click="clearCart" class="clear-cart-btn">清空购物车</button>
    </div>
    <div class="cart-item" v-for="(item, index) in cartItems" :key="item.cartId">
        <div class="item-checkbox">
            <input type="checkbox" v-model="item.selected">
        </div>
        <div class="item-image">
            <img :src="item.imageUrl" alt="商品图片">
        </div>
        <div class="item-info">
            <h3>{{ item.productName }}</h3>
            <p class="item-price">¥{{ item.price.toFixed(2) }}</p>
            <div class="quantity-selector">
                <button @click="decreaseQuantity(index)" :disabled="item.quantity <= 1">-</button>
                <input type="number" v-model="item.quantity" min="1">
                <button @click="increaseQuantity(index)">+</button>
            </div>
            <button @click="removeFromCart(item.productId)" class="remove-btn">删除</button>
        </div>
        <div class="item-subtotal">
            <p>小计: ¥{{ (item.price * item.quantity).toFixed(2) }}</p>
        </div>
    </div>
    <div class="cart-summary">
        <div class="selected-count">
            已选择 <span>{{ selectedCount }}</span> 件商品
        </div>
        <div class="total-price">
            总价: <span class="price">¥{{ totalPrice.toFixed(2) }}</span>
        </div>
        <button @click="showCheckoutModal" class="checkout-btn" :disabled="selectedCount === 0">
            结算 ({{ selectedCount }})
        </button>
    </div>
</div>
<div v-else class="empty-cart">
    <i class="el-icon-shopping-cart-2"></i>
    <p>购物车是空的</p>
    <button @click="goShopping" class="start-shopping-btn">去购物</button>
</div>



<script src="../js/vue.js"></script>
<script src="../js/element-ui/lib/index.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<link rel="stylesheet" href="../js/element-ui/lib/theme-chalk/index.css">
<script>
    var app = new Vue({
        el: '#cart-page',
        data() {
            return {
                cartItems: [

                ],
                userId: 1,
                showCheckout: false,
                selectedAddress: 0
            };
        },
        computed: {
            selectedCount() {
                return this.cartItems.filter(item => item.selected).length;
            },
            totalPrice() {
                return this.cartItems
                    .filter(item => item.selected)
                    .reduce((total, item) => {
                        const price = parseFloat(item.price);
                        const quantity = parseInt(item.quantity);
                        return total + (isNaN(price) || isNaN(quantity)? 0 : price * quantity);
                    }, 0);
            },
            selectedItems() {
                return this.cartItems.filter(item => item.selected);
            }
        },
        methods: {
            async fetchCartItems() {
                try {
                    const response = await axios.get('/shoppingcarts/items', {
                        params: { userId: this.userId }
                    });
                    if (response.data.code === 20041) {
                        this.cartItems = response.data.data.map(item => ({
                            ...item,
                            selected: false
                        }));
                    } else {
                        this.$message.error(response.data.msg || '获取购物车失败');
                    }
                } catch (error) {
                    if (error.response) {
                        this.$message.error(`请求失败，状态码: ${error.response.status}，错误信息: ${error.response.data.msg || '无错误信息'}`);
                    } else if (error.request) {
                        this.$message.error('请求未成功发出，请检查网络连接');
                    } else {
                        this.$message.error('发生其他错误，请联系管理员');
                    }
                    console.error('获取购物车失败', error);
                }
            },
            async handleAddCart() {
                // 添加商品到购物车的逻辑
            },
            async updateCartItemQuantity(productId, quantity) {
                // 更新购物车项数量的逻辑
            },
            decreaseQuantity(index) {
                const item = this.cartItems[index];
                if (item.quantity > 1) {
                    item.quantity--;
                    this.updateCartItemQuantity(item.productId, item.quantity);
                }
            },
            increaseQuantity(index) {
                const item = this.cartItems[index];
                item.quantity++;
                this.updateCartItemQuantity(item.productId, item.quantity);
            },
            removeFromCheckout(index) {
                const item = this.selectedItems[index];
                // 从购物车中移除该商品
                this.cartItems = this.cartItems.filter(cartItem => cartItem.cartId !== item.cartId);
                this.$message.success('已从购物车移除商品');
            },
            async clearCart() {

                // 清空购物车的逻辑

            },
            closeCheckoutModal() {
                this.showCheckout = false;
            },
            showCheckoutModal() {
                if (this.selectedCount === 0) {
                    this.$message.warning('请选择要结算的商品');
                    return;
                }
                this.showCheckout = true;
            },
            submitOrder() {
                // 提交订单的逻辑
                this.$message.success('订单提交成功！');
                this.showCheckout = false;
            },
            goShopping() {
                window.history.back();
            }
        },
        mounted() {
            this.fetchCartItems();
        }
    });
</script>
</body>
</html>